package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.AdminPreid;
import com.ruoyi.system.domain.AdminSubject;
import com.ruoyi.system.service.IAdminPreidService;
import com.ruoyi.system.service.IAdminSubjectService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 课程Controller
 * 
 * @author ruoyi
 * @date 2020-06-16
 */
@Controller
@RequestMapping("/system/tcsubject")
public class AdminTcSubjectController extends BaseController
{
    private String prefix = "system/tcsubject";

    @Autowired
    private IAdminSubjectService adminSubjectService;

    @Autowired
    private IAdminPreidService adminPreidService;
    @RequiresPermissions("system:subject:view")
    @GetMapping()
    public String subject()
    {
      return prefix + "/subject";
    }

    /**
     * 查询课程列表
     */
    @RequiresPermissions("system:subject:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AdminSubject adminSubject)
    {
        startPage();
        List<AdminSubject> list = adminSubjectService.selectAdminSubjectList(adminSubject);
        return getDataTable(list);
    }

    /**
     * 导出课程列表
     */
    @RequiresPermissions("system:subject:export")
    @Log(title = "课程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AdminSubject adminSubject)
    {
        List<AdminSubject> list = adminSubjectService.selectAdminSubjectList(adminSubject);
        ExcelUtil<AdminSubject> util = new ExcelUtil<AdminSubject>(AdminSubject.class);
        return util.exportExcel(list, "subject");
    }

    /**
     * 新增课程
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存课程
     */
    @RequiresPermissions("system:subject:add")
    @Log(title = "课程", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AdminSubject adminSubject)
    {
        return toAjax(adminSubjectService.insertAdminSubject(adminSubject));
    }

    /**
     * 修改课程
     */
    @GetMapping("/edit/{subjectId}")
    public String edit(@PathVariable("subjectId") Long subjectId, ModelMap mmap)
    {
        AdminSubject adminSubject = adminSubjectService.selectAdminSubjectById(subjectId);
        mmap.put("adminSubject", adminSubject);
        return prefix + "/edit";
    }

    /**
     * 修改保存课程
     */
    @RequiresPermissions("system:subject:edit")
    @Log(title = "课程", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AdminSubject adminSubject)
    {
        return toAjax(adminSubjectService.updateAdminSubject(adminSubject));
    }

    /**
     * 删除课程
     */
    @RequiresPermissions("system:subject:remove")
    @Log(title = "课程", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(adminSubjectService.deleteAdminSubjectByIds(ids));
    }

    /**
     * 查询课时详细
     */
    @RequiresPermissions("system:subject:list")
    @GetMapping("/detail/{subjectId}")
    public String detail(@PathVariable("subjectId") Long subjectId, ModelMap mmap)
    {
      AdminPreid adminPreid = new AdminPreid();
      adminPreid.setSubjectId(subjectId);
      mmap.put("preidList", adminPreidService.selectAdminPreidList(adminPreid));
      mmap.put("subjectId", subjectId);
      return "system/preid/preid";
    }
}
